package com.imay.live.adapter;

import android.support.annotation.LayoutRes;
import android.util.SparseArray;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.fragment.dynamics.DynamicsParams;
import com.imay.live.helper.UrlHelper;
import com.imay.live.model.FeedModel;
import com.imay.live.utils.GlideUtil;

import java.util.ArrayList;


/**
 * Created by chan on 2017/3/25.
 */

public class LabelFeedsAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {

    public static final int LABEL = 1;
    public static final int FEED = 2;

    private SparseArray<Integer> layouts;

    private boolean mIsHasHead;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     */
    public LabelFeedsAdapter(boolean isHasHead) {
        super(new ArrayList<Object>());
        addItemType(FEED, R.layout.item_dynamisc_grid);
        addItemType(LABEL, R.layout.item_label);

        mIsHasHead = isHasHead;
    }

    protected void addItemType(int type, @LayoutRes int layoutResId) {
        if (layouts == null) {
            layouts = new SparseArray<>();
        }
        layouts.put(type, layoutResId);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        return createBaseViewHolder(parent, getLayoutId(viewType));
    }

    private int getLayoutId(int viewType) {
        return layouts.get(viewType);
    }

    @Override
    protected int getDefItemViewType(int position) {
        if (!mData.isEmpty()) {
            Object item = mData.get(position);
            if (item instanceof String) {
                return LABEL;
            } else if (item instanceof FeedModel) {
                return FEED;
            }
        }
        return LABEL;
    }

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        String imageUrl = null;
        if (item instanceof FeedModel) {
            FeedModel bean = (FeedModel) item;
            if (bean.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_IMAGE) {
                imageUrl = bean.getImgUrl();
                helper.setVisible(R.id.iv_discovery_item_video, false);
                helper.setVisible(R.id.iv_discovery_item_live, false);
            } else if (bean.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_VIDEO) {
                imageUrl = UrlHelper.getVideoThumbUrl(bean.getVideoUrl());
                helper.setVisible(R.id.iv_discovery_item_video, true);
                helper.setVisible(R.id.iv_discovery_item_live, false);
            }
            if (bean.getFamilyId() != 0 && bean.getInternalPublic() != 0) {
                helper.setVisible(R.id.iv_discovery_item_family, true);
            } else {
                helper.setVisible(R.id.iv_discovery_item_family, false);
            }
            helper.addOnClickListener(R.id.iv_discovery_item_cover);
            Object url;
            if (bean.getRepostedFeed() != null && bean.getRepostedFeed().getUid() == 0) {
                url = R.mipmap.ico_01_nodata;
            } else {
                url = imageUrl;
            }
            GlideUtil.loadRectangleImage((ImageView) helper.getView(R.id.iv_discovery_item_cover), url, R.dimen.size_4dp, R.drawable.shape_placeholder_2);
        } else if (item instanceof String) {
            setFullSpan(helper);
            String str = (String) item;
            helper.setText(R.id.tv_item_label, str);
        }
    }
}
